<?php
/**
 * @name user.php
 * @copyright (c) 2011
 * @author Arsene Tochemey GANDOTE<arsene.gandote@yahoo.com>
 */
class User extends AppModel {
	var $name = 'User';
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $hasAndBelongsToMany = array(
		'Module' => array(
			'className' => 'Module',
			'joinTable' => 'modules_users',
			'foreignKey' => 'user_id',
			'associationForeignKey' => 'module_id',
			'unique' => true,
			'conditions' => array('Module.active'=>1, 'Module.installed'=>1),
			'fields' => '',
			'order' => array('Module.name'=>'ASC'),
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
			)
	);

		# Validation rules.
    var $validate = array(
        'username' => array(
            'notNull' =>array(
    		   'rule' => 'notEmpty',
    		   'required' => true,
    		   'message' => 'The Login Name must not be null and it is required',
    		   'last' => true
    		),
    	    'alphanumeric' => array(
    		   'rule' => 'alphaNumeric',
    		   'required' => true,
    		   'message' => 'Login must be alphanumeric.',
    		   'last' => true
    		)
		),
        'first_name' => array(
            'notNull' =>array(
    		   'rule' => 'notEmpty',
    		   'required' => true,
    		   'message' => 'The Fisrt Name must not be null and it is required',
    		   'last' => true
    		),
    	    'alphanumeric' => array(
    		   'rule' => 'alphaNumeric',
    		   'required' => true,
    		   'message' => 'First Name must be alphanumeric.',
    		   'last' => true
    		)
		),
        'last_name' => array(
            'notNull' =>array(
    		   'rule' => 'notEmpty',
    		   'required' => true,
    		   'message' => 'The Last Name must not be null and it is required',
    		   'last' => true
    		),
    	    'alphanumeric' => array(
    		   'rule' => 'alphaNumeric',
    		   'required' => true,
    		   'message' => 'Last Name must be alphanumeric.',
    		   'last' => true
    		)
		),
	    'password' => array(
	       'rule' => 'notEmpty',
	       'required' => true,
	       'message' => 'The Password must not be null and it is required'
	    )
	);


    /**
     * This function retrieves all the users from the database tables users.
     * @name getAllUsers
     * @param void
     * @return mixed. Array containing the data retrieved.
     * @access public
     */
    function getAllUsers(){
    	# fetch the data and return it.
    	return $this->find('all', array('conditions' => array('User.deleted' => 0), 'recursive' => -1));
    }

    /**
     * This function retrieves the user data based upon his/her id
     * @name getUserById
     * @param string $userId string holding the user id.his parameter is defaulted to null.
     * When it is null it means that cake is going to use the ActiveRecord system to retrieve the data.
     * @return mixed. Array containing the user data.
     * @access public
     */
    function getUserById($userId = null){
        $conditions = array(
           'User.id' => $userId,
           'User.deleted' => 0
        );
        # fetch the specific data from the server and retrun it.
        return  $this->find('first', array('conditions' => $conditions, 'recursive' => 1));
    }
    
    
          # User Data manipulation functions
      /**
       * This function retrieves the user by his login name
       * @name getUserByLoginName
       * @param string $loginName
       * @return array of data containing the login name and the user password
       * @access public
       */
      function getUserByLoginName($loginName = null){
              # search condition
             $conditions = array(
                 'User.username' => $loginName,
                 'User.deleted' => 0
             );
             # fetch the specific data from the server and retrun it.
            return  $this->find('first', array('conditions' => $conditions, 'recursive' => 1));
      }


}
?>
